clear,clc
numIter =10;
conf = 3;
confidence_percent = conf;

path_origdata = '/home/li/Documents/Trainning/SADTestFCMap/DataProcessed';
templatePath = '/home/li/Documents/Trainning/SADTestFCMap/Parcellation_template';
ListPath = '/home/li/Documents/Trainning/SADTestFCMap/Lists';
outputDIR = '/home/li/Documents/Trainning/SADTestFCMap/Results';

SIDs = textread([ListPath '/SubList.txt'],'%s');

combineLeftRight = 1;
%%%%%%%%%%%%%%%%%%%%
for s = 1:length(SIDs)
    sid = SIDs{s};
    lhData=[];
    rhData=[];

%   lh.100803_HP66EU_bld014_rest_reorient_skip_faln_mc_g1000000000_bpss_resid_fsaverage6_sm6_fsaverage4
    Files_LH = dir([path_origdata '/' sid '/surf/lh.*sm6_fs5.nii.gz']);
    Files_RH = dir([path_origdata '/' sid '/surf/rh.*sm6_fs5.nii.gz']);

    fprintf(Files_LH(1).name);
    fprintf('\n');
    fprintf(Files_RH(1).name);
    fprintf('\n');
    for e = 1:length(Files_LH)
        filename_bold_lh = [Files_LH(e).name];
        fullfilename_bold_lh = [path_origdata '/'  sid '/surf/' filename_bold_lh];

        filename_bold_rh = [Files_RH(e).name];
        fullfilename_bold_rh = [path_origdata '/'  sid '/surf/' filename_bold_rh];

        lh_hdr = MRIread(fullfilename_bold_lh);
        lhData_raw = reshape(lh_hdr.vol,[size(lh_hdr.vol,1)*size(lh_hdr.vol,2)*size(lh_hdr.vol,3), size(lh_hdr.vol,4)]);

        rh_hdr = MRIread(fullfilename_bold_rh);
        rhData_raw = reshape(rh_hdr.vol,[size(rh_hdr.vol,1)*size(rh_hdr.vol,2)*size(rh_hdr.vol,3), size(rh_hdr.vol,4)]);

        % lhData_run= butterfilt_surf(lhData_raw, 1/0.72);
        % rhData_run= butterfilt_surf(rhData_raw, 1/0.72);
        %
        lhData_run = lhData_raw;
        lhData = [lhData lhData_run];

        rhData_run = rhData_raw;
        rhData = [rhData rhData_run];

        templateFile = dir([templatePath '/*_asym_fs5_Patch.mgh']);
        for t = 1:length(templateFile)
            % 计算左脑区
            filename_tem_lh = [templateFile(t).name];
            fullfilename_tem_lh = [templatePath '/' filename_tem_lh];

            [vol, M, mr_parms, volsz] = load_mgh(fullfilename_tem_lh);
            if (strfind(filename_tem_lh, '17'))
                [annot_v, annot_L, cto]= read_annotation(['/usr/local/freesurfer/subjects/fsaverage5/label/lh.aparc.annot']);
                % acc label
                acc_str_ID = cto.table(27, 5);
                temp_indices = find(annot_L==acc_str_ID);
                temp_indices(47,:) = [];
                
                fprintf('Template 17: ');
                fprintf(filename_tem_lh);
                fprintf('\n');
            else
                temp_indices = find(vol==1);
                % tmp_roi_indices = find(temp_indices==3028);
                % temp_indices = temp_indices(tmp_roi_indices);
                fprintf('Template 19: ');
                fprintf(filename_tem_lh);
                fprintf('\n');
            end

            roi_rows = lhData_raw(temp_indices, :);
            mean_roi = mean(roi_rows, 1);

            RData_LH = [];
            for v = 1:length(vol)
                [r, p] = corr((mean_roi)', lhData_raw(v, :)');
                RData_LH = [RData_LH r];
            end

            RData_LH = RData_LH';
            output_file = strrep(filename_tem_lh, '.mgh', '_FC.mgh')
            save_mgh(RData_LH, [outputDIR '/' 'lh_' sid '_' output_file], eye(4));

            % 计算右脑区
            RData_RH = [];
            for v = 1:length(vol)
                [r, p] = corr((mean_roi)', rhData_raw(v, :)');
                RData_RH = [RData_RH r];
            end

            RData_RH = RData_RH';
            output_file = strrep(filename_tem_lh, '.mgh', '_FC.mgh')
            save_mgh(RData_RH, [outputDIR '/' 'rh_' sid '_' output_file], eye(4));
        end
    end
end
